<template>
  <el-dialog
    :visible.sync="visible"
    width="40%"
    append-to-body
    :title="!dataForm.id ? '新增' : '修改'">
    <el-form :model="dataForm" ref="dataForm" label-width="60px" :rules="dataRule">
      <el-form-item label="标题" prop="name">
        <el-input v-model="dataForm.name"/>
      </el-form-item>
      <el-form-item label="简介" prop="brief">
        <el-input type="textarea" v-model="dataForm.brief"/>
        <span>例：登录可获得xx积分</span>
      </el-form-item>
      <el-form-item label="排序" prop="name">
        <el-input-number v-model="dataForm.sort"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitForm()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    name: 'pointadd',
    data() {
      return {
        loading: false,
        visible: false,
        dataForm: {
          brief: '',
          name: '',
          id: '',
          sort: 0
        },
        dataRule: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
          brief: [{required: true, message: '简介不能为空', trigger: 'blur'}]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/integral-admin/rule/app/info/${this.dataForm.id}`),
              method: 'post'
            }).then(res => {
              if (res.code === 200) {
                this.dataForm = res.data
              }
            })
          }
        })
      },
      submitForm() {
        this.loading = true
        this.$refs['dataForm'].validate(async valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/integral-admin/rule/app/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: {
                id: this.dataForm.id,
                name: this.dataForm.name,
                brief: this.dataForm.brief,
                sort: this.dataForm.sort
              }
            }).then(res => {
              if (res.code === 200) {
                this.loading = false
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.loading = false
              }
            })
          } else {
            this.loading = false
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
